/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * editorView.java
 *
 * Created on 16/05/2011, 17:11:43
 */

package presentation;
import java.awt.Graphics;
import java.util.ArrayList;
import domain.Pair;
import javax.swing.DefaultListModel;
import javax.swing.JTextArea;
import javax.swing.text.StyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import java.awt.Color;
import java.awt.GridLayout;
import java.util.StringTokenizer;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
 *
 * @author Andreu Marimon
 */
public class EditorView extends GraficConf {
    XinxetoTextView _textXinxeto;
    PresentationController _pCntrl;
    private final static String PUNCTUATION_MARKS = "?!.;,:()[]{}<>\"\'—¿¡«»*";

    private Boolean isPunctuationMark(char c) {
        return PUNCTUATION_MARKS.indexOf(c) != -1;
    }

    Pair <Integer, Integer> wordPositionToCharPosition (String text, int position) {
	text = text.replaceAll("\n\r","");
	text = text.replaceAll("\n"," ");
	for (int i = 0; i < PUNCTUATION_MARKS.length(); ++i) {
	    text = text.replace(PUNCTUATION_MARKS.charAt(i), '!');
	}
	//textPane.setText(text);
	//System.out.println(text);
	String[] words = text.split(" ");
	int lenght, i;
	lenght = i = 0;

	for (;i <position-1; ++i) {
	    lenght += words[i].length();
	    if (words[i].length() == 0 || words[i].charAt(0) == '!' && words[i].length() == 1 ) ++position;
	}
	for (; words[i].length() == 0;++i) ++ position;
	int wordLenght = 0;
	for (int j = 0; j <words[position-1].length();++j) {
	    if (!isPunctuationMark(words[position-1].charAt(j))) ++wordLenght;
	}
	Pair <Integer, Integer> result = new Pair <Integer, Integer>(lenght+i,wordLenght);
	return result;
    }

    private void selectVerb (String text, int position) {
	textPane.requestFocus();
	textPane.setCaretPosition(wordPositionToCharPosition (text, position).first);
	textPane.moveCaretPosition (wordPositionToCharPosition (text, position).first+wordPositionToCharPosition (text, position).second);
    }

    private void paintverbs (String text, int position) {
	styleDoc.setCharacterAttributes(wordPositionToCharPosition (text, position).first, wordPositionToCharPosition (text, position).second, textPane.getStyle("Color Underline"), true);
    }

    private void clickNominalization (int pos) {
	    model.removeAllElements();
	    favoriteButton.setEnabled(false);
	    ArrayList< Pair<String,Float> > result = new ArrayList< Pair<String,Float> >();
	    result = _pCntrl.clickVerb(pos);
	    for (int i = 0; i < result.size(); ++i) {
		model.addElement(result.get(i).first);
	    }
	    selectVerb(textPane.getText(),_pCntrl.getCurrentPosition());
    }

    private void startNominalization() {
	if (initNomButton.getText().equals("edit text")) {
	    textPane.setEditable(true);
	    String text = textPane.getText();
	    model.removeAllElements();
	    textPane.requestFocus();
	    textPane.setCaretPosition(0);
	    styleDoc.setCharacterAttributes(0, text.length(), textPane.getStyle("Black noUnderline"), true);
	    styleDoc.setLogicalStyle(0, textPane.getStyle("Black noUnderline"));
	    nextButton.setEnabled(false);
	    previousButton.setEnabled(false);
		newButton.setEnabled(true);
		openButton.setEnabled(true);
		saveButton.setEnabled(true);
		statisticsButton.setEnabled(true);
		helpButton.setEnabled(true);
		credits.setEnabled(true);
		preferencesButton.setEnabled(true);
	    initNomButton.setText("Initialize");
	}
	else {
	    model.removeAllElements();
	    favoriteButton.setEnabled(false);
	    ArrayList< Pair<String,Float> > result = new ArrayList< Pair<String,Float> >();
	    String text = textPane.getText();
	    result = _pCntrl.nominalize(text);
		if (result.isEmpty()) {
			JOptionPane.showMessageDialog(textPane, "No verbs detected", "Info", 1);
		}
	    styleDoc.setCharacterAttributes(0, text.length(), textPane.getStyle("Black noUnderline"), true);
	    ArrayList<Integer> positions = _pCntrl.getPositions();
	    for (Integer singleposition: positions) {
		paintverbs(textPane.getText(), singleposition);
	    }
	    if (!positions.isEmpty()) {
		for (int i = 0; i < result.size(); ++i) {
		    model.addElement(result.get(i).first);
		}
		textPane.setEditable(false);
		selectVerb(textPane.getText(),_pCntrl.getCurrentPosition());
		nextButton.setEnabled(true);
		previousButton.setEnabled(true);
		newButton.setEnabled(false);
		openButton.setEnabled(false);
		saveButton.setEnabled(false);
		statisticsButton.setEnabled(false);
		helpButton.setEnabled(false);
		credits.setEnabled(false);
		preferencesButton.setEnabled(false);
		initNomButton.setText("edit text");
	    }
	}
    }

    private void restoreContext() {
	textPane.setText(_pCntrl.getText());
	if (_pCntrl.getContextNominalized()) {
	    startNominalization();
	    clickNominalization(_pCntrl.getContextCurrentPosition());
	}
    }
    public EditorView(PresentationController pCntrl) {
	super (pCntrl);
        _pCntrl = pCntrl;
        initComponents();
	_textXinxeto = new XinxetoTextView(_pCntrl);
	jPanel2.setLayout(new BorderLayout());
	jPanel2.add(_textXinxeto, BorderLayout.CENTER);
	validate();
	_textXinxeto.setXinxetoText(1);
	_textXinxeto.repaint();
	String strColor = _pCntrl.getTextColor();
	Color color = Color.BLUE;
	if (strColor.equals("BLUE")) color = Color.BLUE;
	else if(strColor.equals("GREEN")) color = Color.GREEN;
	else if(strColor.equals("ORANGE")) color = Color.ORANGE;
	else if(strColor.equals("RED")) color = Color.RED;
	int size = pCntrl.getTextSize();
	styleDoc = textPane.getStyledDocument();

	Style style = textPane.addStyle("Color", null);
	StyleConstants.setForeground(style, color);
	style = textPane.addStyle("Color Underline", style);
	StyleConstants.setUnderline(style, true);
	style = textPane.addStyle("Color Underline", style);
	StyleConstants.setFontSize(style, size);

	style = textPane.addStyle("Black", null);
	StyleConstants.setForeground(style, Color.BLACK);
	style = textPane.addStyle("Black noUnderline", style);
	StyleConstants.setUnderline(style, false);
	style = textPane.addStyle("Black noUnderline", style);
	StyleConstants.setFontSize(style, size);

	styleDoc.setLogicalStyle(0,textPane.getStyle("Black noUnderline"));
	nextButton.setEnabled(false);
	previousButton.setEnabled(false);
	restoreContext();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        textPane = new javax.swing.JTextPane();
        favoriteButton = new defaultJButton();
        initNomButton = new defaultJButton();
        nextButton = new defaultJButton();
        previousButton = new defaultJButton();
        openButton = new defaultJButton();
        newButton = new defaultJButton();
        saveButton = new defaultJButton();
        preferencesButton = new defaultJButton();
        helpButton = new defaultJButton();
        credits = new defaultJButton();
        logoutButton = new defaultJButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        model = new DefaultListModel();
        nominList = new javax.swing.JList(model);
        statisticsButton = new defaultJButton();
        addVerbButton = new defaultJButton();
        jPanel1 = new XinxetoView(_pCntrl);
        jPanel2 = new XinxetoTextView(_pCntrl);

        setFont(new java.awt.Font("Verdana", 1, 11));
        setMaximumSize(new java.awt.Dimension(800, 600));
        setMinimumSize(new java.awt.Dimension(800, 600));

        textPane.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                textPaneMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(textPane);

        favoriteButton.setText("raise preference");
        favoriteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                favoriteButtonActionPerformed(evt);
            }
        });

        initNomButton.setText("Initialize ");
        initNomButton.setActionCommand("Initialize");
        initNomButton.setPreferredSize(new java.awt.Dimension(120, 29));
        initNomButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                initNomButtonActionPerformed(evt);
            }
        });

        nextButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/nextverb.png"))); // NOI18N
        nextButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/nextverb.png"))); // NOI18N
        nextButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/nextverb.png"))); // NOI18N
        nextButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/nextverb.png"))); // NOI18N
        nextButton.setRolloverSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/nextverb.png"))); // NOI18N
        nextButton.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/nextverb.png"))); // NOI18N
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        previousButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/previous verb.png"))); // NOI18N
        previousButton.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/previous verb.png"))); // NOI18N
        previousButton.setDisabledSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/previous verb.png"))); // NOI18N
        previousButton.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/previous verb.png"))); // NOI18N
        previousButton.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/previous verb.png"))); // NOI18N
        previousButton.setRolloverSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/previous verb.png"))); // NOI18N
        previousButton.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/imatges/previous verb.png"))); // NOI18N
        previousButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousButtonActionPerformed(evt);
            }
        });

        openButton.setText("open");
        openButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openButtonActionPerformed(evt);
            }
        });

        newButton.setText("new");
        newButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newButtonActionPerformed(evt);
            }
        });

        saveButton.setText("save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        preferencesButton.setText("preferences");
        preferencesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                preferencesButtonActionPerformed(evt);
            }
        });

        helpButton.setText("help");
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });

        credits.setText("credits");
        credits.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                creditsActionPerformed(evt);
            }
        });

        logoutButton.setText("logout");
        logoutButton.setMaximumSize(null);
        logoutButton.setMinimumSize(null);
        logoutButton.setPreferredSize(new java.awt.Dimension(120, 29));
        logoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutButtonActionPerformed(evt);
            }
        });

        nominList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        nominList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                nominListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(nominList);

        statisticsButton.setText("statistics");
        statisticsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                statisticsButtonActionPerformed(evt);
            }
        });

        addVerbButton.setText("add verb");
        addVerbButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addVerbButtonActionPerformed(evt);
            }
        });

        jPanel1.setMaximumSize(new java.awt.Dimension(112, 108));
        jPanel1.setMinimumSize(new java.awt.Dimension(112, 108));
        jPanel1.setOpaque(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 112, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 103, Short.MAX_VALUE)
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 220, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 74, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(32, 32, 32)
                                .addComponent(initNomButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(49, 49, 49)
                                .addComponent(nextButton, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(statisticsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(20, 20, 20)
                                    .addComponent(logoutButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(addVerbButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 421, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(newButton, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(openButton, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(preferencesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(helpButton, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(credits, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap())
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(favoriteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(86, 86, 86))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(57, 57, 57)))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(credits, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(helpButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(openButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(preferencesButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(nextButton)
                    .addComponent(previousButton)
                    .addComponent(initNomButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(statisticsButton)
                            .addComponent(addVerbButton)
                            .addComponent(logoutButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(favoriteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(21, 21, 21))
        );

        initNomButton.getAccessibleContext().setAccessibleName("Initialize");
    }// </editor-fold>//GEN-END:initComponents

    private void favoriteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_favoriteButtonActionPerformed
	_textXinxeto.setXinxetoText(4);
	_textXinxeto.repaint();
	//System.out.println(nominList.getMaxSelectionIndex());
	_pCntrl.increasePreference(nominList.getMaxSelectionIndex());
	
	JPanel p = new JPanel();
	p.setLayout(new GridLayout(2,1));
	JLabel a = new JLabel("Noun preference increased");
	JLabel b = new JLabel("*you need to logout in order to see changes");
	p.add(a);
	p.add(b);
	JOptionPane.showMessageDialog(textPane, p, "Info", 1);
    }//GEN-LAST:event_favoriteButtonActionPerformed

    private void logoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutButtonActionPerformed
        if (textPane.getText().length()!=0) {
			int a = JOptionPane.showConfirmDialog(textPane,"Do you want to save the current text?","Save",JOptionPane.YES_NO_OPTION,JOptionPane.PLAIN_MESSAGE);
			if(a == JOptionPane.YES_OPTION) {
				JTextArea aux = new JTextArea();
				aux.setText(textPane.getText());
				_pCntrl.save(aux);
			}
		}   
	    _pCntrl.saveContext("", false);
	    _pCntrl.logOut();
    }//GEN-LAST:event_logoutButtonActionPerformed

	
    private void initNomButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_initNomButtonActionPerformed
	startNominalization();
	validate();
	_textXinxeto.setXinxetoText(2);
	_textXinxeto.repaint();
    }//GEN-LAST:event_initNomButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
	_textXinxeto.setXinxetoText(5);
	_textXinxeto.repaint();
	model.removeAllElements();
	favoriteButton.setEnabled(false);
	ArrayList< Pair<String,Float> > result = new ArrayList< Pair<String,Float> >();
	result = _pCntrl.nextVerb();
	for (int i = 0; i < result.size(); ++i) {
	    model.addElement(result.get(i).first);
	}
	selectVerb(textPane.getText(),_pCntrl.getCurrentPosition());
    }//GEN-LAST:event_nextButtonActionPerformed

    private void previousButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previousButtonActionPerformed
	_textXinxeto.setXinxetoText(5);
	_textXinxeto.repaint();
	model.removeAllElements();
	favoriteButton.setEnabled(false);
	ArrayList< Pair<String,Float> > result = new ArrayList< Pair<String,Float> >();
	result = _pCntrl.previousVerb();
	for (int i = 0; i < result.size(); ++i) {
	    model.addElement(result.get(i).first);
	}
	selectVerb(textPane.getText(),_pCntrl.getCurrentPosition());
    }//GEN-LAST:event_previousButtonActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
	//if (textPane.getText().length()!=0) _pCntrl.showError("Are you sure?");
        /*else*/
		String text = textPane.getText();
		if(text.length() > 0) {
			int a = JOptionPane.showConfirmDialog(textPane,"Do you want to save the current text?","Save",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
			if(a == JOptionPane.YES_OPTION) {
				JTextArea aux = new JTextArea();
				aux.setText(textPane.getText());
				_pCntrl.save(aux);
			}
			else if(a== JOptionPane.NO_OPTION) {
				model.removeAllElements();
				favoriteButton.setEnabled(false);
				textPane.setText("");
			}
		}
    }//GEN-LAST:event_newButtonActionPerformed

    private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
	String text = textPane.getText();
		if(text.length() > 0) {
			int a = JOptionPane.showConfirmDialog(textPane,"Do you want to save the current text?","Save",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
			if(a == JOptionPane.YES_OPTION) {
				JTextArea aux = new JTextArea();
				aux.setText(textPane.getText());
				_pCntrl.save(aux);
				model.removeAllElements();
				favoriteButton.setEnabled(false);
				textPane.setText(_pCntrl.open());
			}
			else if(a == JOptionPane.NO_OPTION){
				model.removeAllElements();
				favoriteButton.setEnabled(false);
				textPane.setText(_pCntrl.open());
			}
		}
		else {
			model.removeAllElements();
			favoriteButton.setEnabled(false);
			textPane.setText(_pCntrl.open());
		}

    }//GEN-LAST:event_openButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
		JTextArea aux = new JTextArea();
		aux.setText(textPane.getText());
		_pCntrl.save(aux);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void nominListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nominListMouseClicked
	if (_pCntrl.getId() != 0) {
            favoriteButton.setEnabled(true);
        }
    }//GEN-LAST:event_nominListMouseClicked

    private void textPaneMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_textPaneMouseClicked
	if (initNomButton.getText().equals("edit text")) {
	    int position = textPane.viewToModel(evt.getPoint());
	    String text = textPane.getText();
	    text = text.substring(0, position);
	    StringTokenizer text2 = new StringTokenizer (text);
	    clickNominalization(text2.countTokens());
	}
    }//GEN-LAST:event_textPaneMouseClicked

    private void preferencesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_preferencesButtonActionPerformed
	_pCntrl.saveContext(textPane.getText(), nextButton.isEnabled());
	_pCntrl.goToPreferences();
    }//GEN-LAST:event_preferencesButtonActionPerformed

	private void statisticsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_statisticsButtonActionPerformed
		_pCntrl.saveContext(textPane.getText(), nextButton.isEnabled());
		_pCntrl.goToStatistics();
	}//GEN-LAST:event_statisticsButtonActionPerformed

	private void addVerbButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addVerbButtonActionPerformed
		_textXinxeto.setXinxetoText(3);
		_textXinxeto.repaint();
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(3,1));
		JLabel verb = new JLabel("infinitive:");
		JLabel info = new JLabel("*you need to logout in order to see changes");
		JTextField textField = new JTextField();
		panel.add(verb);
		panel.add(textField);
		panel.add(info);
		int a = JOptionPane.showConfirmDialog(textPane,panel,"add verb",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE);
		if(a == JOptionPane.OK_OPTION) {
			_pCntrl.addVerbToDict(textField.getText());
		}
	}//GEN-LAST:event_addVerbButtonActionPerformed

	private void creditsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_creditsActionPerformed
		JPanel noms = new JPanel();
		noms.setLayout(new GridLayout(4,1));

		JLabel am = new JLabel("Andreu Marimon\n");
		JLabel mm = new JLabel("Marc Molins");
		JLabel jv = new JLabel("Jose Villaverde");
		JLabel oc = new JLabel("Oriol Caño");
		
		noms.add(oc);
		noms.add(am);
		noms.add(mm);
		noms.add(jv);
		
		JOptionPane.showMessageDialog(this, noms, "Credits", JOptionPane.PLAIN_MESSAGE);
	}//GEN-LAST:event_creditsActionPerformed

	private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
		_pCntrl.goToHelp();
	}//GEN-LAST:event_helpButtonActionPerformed

    private DefaultListModel model;
    StyledDocument styleDoc;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addVerbButton;
    private javax.swing.JButton credits;
    private javax.swing.JButton favoriteButton;
    private javax.swing.JButton helpButton;
    private javax.swing.JButton initNomButton;
    private javax.swing.JPanel jPanel1;
    public javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton logoutButton;
    private javax.swing.JButton newButton;
    private javax.swing.JButton nextButton;
    private javax.swing.JList nominList;
    private javax.swing.JButton openButton;
    private javax.swing.JButton preferencesButton;
    private javax.swing.JButton previousButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton statisticsButton;
    private javax.swing.JTextPane textPane;
    // End of variables declaration//GEN-END:variables

    @Override
    public void paint (Graphics g) {
        super.paint(g);
        if (_pCntrl.getId() == 0) {
            favoriteButton.setEnabled(false);
            preferencesButton.setEnabled(false);
			addVerbButton.setEnabled(false);
        }
	if (nominList.getMaxSelectionIndex() == -1) favoriteButton.setEnabled(false);
    }

}

